// NewsReader.cpp : Implementation of CNewsReader
#include "stdafx.h"
#include "NewsReaderBand.h"
#include "NewsReader.h"
#include <assert.h>

extern Provider				g_ProviderEntry;

/////////////////////////////////////////////////////////////////////////////
// CNewsReader

CNewsReader::CNewsReader()
{
	m_pSite = NULL;
	m_hParentWnd = NULL;
}

CNewsReader::~CNewsReader()
{
	if (m_pSite) {
		m_pSite->Release();
	}
}

/////////////////////////////////////////////////////////////////////////////
// IOleWindow implementation
//
STDMETHODIMP CNewsReader::GetWindow (HWND* phWnd)
{
	*phWnd = m_hWnd;
	return S_OK;
}

STDMETHODIMP CNewsReader::ContextSensitiveHelp (BOOL bEnterMode)
{
	return E_NOTIMPL;
}

///////////////////////////////////////////////////////////////////////////
//
// IDockingWindow Implementation
//
STDMETHODIMP CNewsReader::ShowDW (BOOL fShow)
{
	assert (m_hWnd);
	
	if (fShow) {
		ShowWindow(SW_SHOW);
	} else {
		ShowWindow(SW_HIDE);
	}
	return S_OK;
}

STDMETHODIMP CNewsReader::CloseDW (DWORD dwReserved)
{
	ShowDW(FALSE);

	if(::IsWindow(m_hWnd))
		::DestroyWindow(m_hWnd);

	m_hWnd = NULL;
   
	return S_OK;
}

STDMETHODIMP CNewsReader::ResizeBorderDW (LPCRECT prcBorder, IUnknown* punkToolbarSite, BOOL fReserved)
{
	// this function is never used and shall always return E_NOTIMPL.
	return E_NOTIMPL;
}

///////////////////////////////////////////////////////////////////////////
//
// IObjectWithSite implementations
//
STDMETHODIMP CNewsReader::SetSite (IUnknown* punkSite)
{
	if (m_pSite) {
		m_pSite->Release();
		m_pSite = NULL;
	}

	if (!punkSite) {
		return S_OK;
	}

	if(!SUCCEEDED(punkSite->QueryInterface(IID_IOleWindow, (LPVOID*)&m_pSite))) {
		return E_FAIL;
	}

	m_pSite->GetWindow(&m_hParentWnd);
	if (!m_hParentWnd) {
		return E_FAIL;
	}
	return CreateBandWindow();
}

STDMETHODIMP CNewsReader::GetSite (REFIID riid, LPVOID *ppvReturn)
{
	*ppvReturn = NULL;
	if(m_pSite) {
		return m_pSite->QueryInterface(riid, ppvReturn);
	}
	return E_FAIL;
}

///////////////////////////////////////////////////////////////////////////
//
// IPersistStream implementations
// 
STDMETHODIMP CNewsReader::GetClassID(LPCLSID pClassID)
{
	*pClassID = CLSID_NewsReader;
	return S_OK;
}

STDMETHODIMP CNewsReader::IsDirty(void)
{
	return S_FALSE;
}

STDMETHODIMP CNewsReader::Load(LPSTREAM pStream)
{
	return S_OK;
}

STDMETHODIMP CNewsReader::Save(LPSTREAM pStream, BOOL fClearDirty)
{
	return S_OK;
}

STDMETHODIMP CNewsReader::GetSizeMax(ULARGE_INTEGER *pul)
{
	return E_NOTIMPL;
}

///////////////////////////////////////////////////////////////////////////
//
// IDeskBand Implementation
//
STDMETHODIMP CNewsReader::GetBandInfo(DWORD dwBandID, DWORD dwViewMode, DESKBANDINFO* pdbi)
{
	if( NULL == pdbi ) {
		return E_INVALIDARG;
	}

	if(pdbi->dwMask & DBIM_MINSIZE) {	
		// minimal size of the band
		pdbi->ptMinSize.x = 200;
		pdbi->ptMinSize.y = 20;
	}

	if(pdbi->dwMask & DBIM_MAXSIZE) {
		// maximun size is (-1) 4G
		pdbi->ptMaxSize.x = -1;
		pdbi->ptMaxSize.y = -1;
	}

	if(pdbi->dwMask & DBIM_INTEGRAL) {
		// sizing step is 1.
		pdbi->ptIntegral.x = 1;
		pdbi->ptIntegral.y = 1;
	}

	if(pdbi->dwMask & DBIM_ACTUAL) {
		// container can decide the size. No preference.
		pdbi->ptActual.x = 0;
		pdbi->ptActual.y = 0;
	}

	if(pdbi->dwMask & DBIM_TITLE) {	
		wcscpy(pdbi->wszTitle, L"News Reader");
	}

	if(pdbi->dwMask & DBIM_MODEFLAGS) {
		// resizable;
		pdbi->dwModeFlags = DBIMF_VARIABLEHEIGHT;
	}

	if(pdbi->dwMask & DBIM_BKCOLOR) {	
		// using default color.
		pdbi->dwMask &= ~DBIM_BKCOLOR;
	}

	return S_OK;
}

/////////////////////////////////////////////////
// Non COM functions
//
HRESULT	CNewsReader::CreateBandWindow()
{
	assert (m_hParentWnd);

	RECT rc = {0, 0, 200, 20};
	Create(m_hParentWnd, rc);

   
	AddProvider(g_ProviderEntry.pvdr);
	SetMoveStep(5);
	return S_OK;
}

